package com.letchat.service.impl;

import com.letchat.controller.RegisterController;
import com.letchat.mapper.RegisterMapper;
import com.letchat.mapper.UserMapper;
import com.letchat.pojo.Email;
import com.letchat.pojo.User;
import com.letchat.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Service
public class RegisterService {
    @Autowired
    private JavaMailSender javaMailSender;
    @Value("${spring.mail.username}") //动态获取配置文件中 发送邮件的账户
    private String sendemail;
    @Autowired
    private RegisterMapper captchaMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 获取验证码
     *
     * @param email
     * @return
     */
    public Email getCaptcha(String email) {
        int captcha = new Random().nextInt(9999) % (9999 - 1000 + 1) + 1000;
        //创建简单邮件消息
        SimpleMailMessage message = new SimpleMailMessage();
        //谁发的
        message.setFrom(sendemail);
        //谁要接收
        message.setTo(email);
        //邮件标题
        message.setSubject("Letchat验证码");
        //邮件内容
        message.setText("您的验证码是" + captcha);

        if (captchaMapper.getByEmail(email) == null) {
            captchaMapper.save(new Email(null, null, email, String.valueOf(captcha), LocalDateTime.now()));
        } else {
            captchaMapper.updateCaptchaAndTime(email, String.valueOf(captcha), LocalDateTime.now());
        }

        javaMailSender.send(message);

        return captchaMapper.getByEmail(email);
    }

    /**
     * 注册
     *
     * @param email
     * @param captcha
     * @return
     */
    public String register(String email, String captcha) {
        Email e = captchaMapper.getByEmailAndCaptcha(email, captcha);
        if (e != null && ChronoUnit.MINUTES.between(e.getUpdateTime(), LocalDateTime.now()) <= 1) {
            if (e.getUserId() != null) return null;
            String username = "用户" + (new Random().nextInt(9999) % (9999 - 1000 + 1) + 1000);
            String password = String.valueOf(new Random().nextInt(999999) % (999999 - 100000 + 1) + 100000);
            User u = new User(null, username, password, username, null, email);
            userMapper.save(u);
            captchaMapper.setUserIdByEmail(u.getId(), email);
            u.setImgCode((short) 1);

            Map<String, Object> claim = new HashMap<>();
            claim.put("id", u.getId());
            claim.put("name", u.getName());
            claim.put("imgCode", u.getImgCode());

            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(sendemail);
            message.setTo(email);
            message.setSubject("Letchat用户名和密码");
            message.setText("您的用户名是：" + u.getUsername() + "\n" + "您的密码是：" + u.getPassword() + "\n" + "请妥善保管");
            javaMailSender.send(message);

            return JwtUtils.generateJwt(claim);
        }
        return null;
    }


    /**
     * 判断邮箱是否已存在
     *
     * @param email
     * @return
     */
    public boolean emailIsExist(String email) {
        return captchaMapper.getByEmail(email) != null;
    }
}
